package org.huluobo.utils;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

/**
 * 短信验证码工具类
 * 
 * @author SunTo
 * 
 */
public class SMSUtil {
	private static String url = "http://gw.api.taobao.com/router/rest";
	private static String appkey = "23761944";
	private static String secret = "9813352b63d3b60570ac895efa2c3101";

	/**
	 * 发送验证码
	 * 
	 * @return
	 */
	public static boolean sendSMS(String code,String phone) {
		try {
			TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
			AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
			req.setExtend("");
			req.setSmsType("normal");
			req.setSmsFreeSignName("胡萝博");
			req.setSmsParamString("{number:'" + code + "'}");
			req.setRecNum(phone);
			req.setSmsTemplateCode("SMS_62740126");
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
			// System.out.println(rsp.getResult().getSuccess());
			if (rsp.getResult().getSuccess()) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 随机生成6位数字作为验证码
	 * 
	 * @return
	 */
	public static String getCode() {
		int code = (int) (Math.random() * 9000 + 100000);
		return code + "";
	}
}
